/*
 * IndexTag.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the org.geek.login.web, Version 1.0 (the "License");
 *
 */
package org.geek.login.web;

import java.util.ArrayList;
import java.util.List;

import org.geek.common.entity.ExtTree;
import org.geek.common.tag.BaseTag;

/**
 * <p></p>
 * @author ninglong bhd2973@qq.com
 * @version V1.0
 */
public class IndexTag extends BaseTag {

	private static final long serialVersionUID = 8721048499971893684L;
	
	private List<ExtTree> list = new ArrayList<ExtTree>(); 
	
	@Override
	protected void init() {

	}

	@Override
	protected void destory() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void genernateJavaScript(StringBuffer buffer) {
		buffer.append("	Ext.onReady(function(){																						").append("\r\n");
		buffer.append("		Ext.QuickTips.init();																					").append("\r\n");
		
		initPortalTools(buffer);
		
		buffer.append("		new Ext.Viewport({																						").append("\r\n");
		buffer.append("			layout: 'border',																					").append("\r\n");
		buffer.append("			items: [																							").append("\r\n");
		
		initNorth(buffer);
		
		buffer.append("			,																									").append("\r\n");
		
		initSouth(buffer);
		
		buffer.append("			, {																									").append("\r\n");
		buffer.append("				region: 'west',																					").append("\r\n");
		buffer.append("				id: 'west-panel',																				").append("\r\n");
		buffer.append("				title: '系统导航',																					").append("\r\n");
		buffer.append("				iconCls:'icon-program',																			").append("\r\n");
		buffer.append("				split: true,																					").append("\r\n");
		buffer.append("				margins:'0 0 0 2',																				").append("\r\n");
		buffer.append("				width: 200,																						").append("\r\n");
		buffer.append("				collapsible: true,																				").append("\r\n");
		buffer.append("				layout: {																						").append("\r\n");
		buffer.append("					type: 'accordion',																			").append("\r\n");
		buffer.append("					animate: true																				").append("\r\n");
		buffer.append("				}																								").append("\r\n");
		if(!list.isEmpty()){
			buffer.append("				,items: [																					").append("\r\n");
			int size = list.size();
			for(int i=0;i<size;i++){
				ExtTree tree = list.get(i);
				initModuleTree(buffer,tree);
				if(i != size-1){
						buffer.append("	, 																							").append("\r\n");
				}
			}
			buffer.append("				]																							").append("\r\n");
		}
		buffer.append("			},																									").append("\r\n");
		buffer.append("				new Ext.TabPanel({																				").append("\r\n");
		buffer.append("					region: 'center',																			").append("\r\n");
		buffer.append("					deferredRender: false,																		").append("\r\n");
		buffer.append("					id:'"+this.getId("tabs_main")+"',															").append("\r\n");
		buffer.append("					enableTabScroll: true, 																		").append("\r\n");
		buffer.append("					margins:'0 2 0 0',																			").append("\r\n");
		buffer.append("					activeTab: 0,																				").append("\r\n");
		buffer.append("					plugins: new Ext.ux.TabCloseMenu({															").append("\r\n");
		buffer.append("						closeTabText: '关闭标签页',																	").append("\r\n");
		buffer.append("						closeOtherTabsText: '关闭其他标签页',														").append("\r\n");
		buffer.append("						closeAllTabsText: '关闭所有标签页'															").append("\r\n");
		buffer.append("					}),																							").append("\r\n");
		buffer.append("					listeners: {																				").append("\r\n");
		buffer.append("						'bodyresize': function (panel, neww, newh) {											").append("\r\n");
		// 自动调整tab下面的panel的大小
		buffer.append("							var centerpanel = Ext.getCmp(panel.getActiveTab().id+'_panel');						").append("\r\n");
		buffer.append("							if (centerpanel) {																	").append("\r\n");
		buffer.append("								centerpanel.setHeight(newh - 2);												").append("\r\n");
		buffer.append("								centerpanel.setWidth(neww - 2);													").append("\r\n");
		buffer.append("							}																					").append("\r\n");
		buffer.append("						}																						").append("\r\n");
		buffer.append("					},																							").append("\r\n");
		buffer.append("					items: [{																					").append("\r\n");
		buffer.append("						title: '主页',																			").append("\r\n");
		buffer.append("						iconCls:'icon-home',																	").append("\r\n");
		buffer.append("						autoScroll: true,																		").append("\r\n");
		buffer.append("						xtype: 'portal',																		").append("\r\n");
		buffer.append("						items:[{																				").append("\r\n");
		
		initPortalColunm(buffer);
		
		buffer.append("						},{																						").append("\r\n");
		
		initPortalColunm(buffer);
		
		buffer.append("						},{																						").append("\r\n");
		
		initPortalColunm(buffer);
		
		buffer.append("						}]																						").append("\r\n");
		buffer.append("				}]																								").append("\r\n");
		buffer.append("			})]																									").append("\r\n");
		buffer.append("		});																										").append("\r\n");
		initClick(buffer);
		buffer.append(" });																											").append("\r\n");
	}

	@Override
	protected void genernateHtml(StringBuffer buffer) {
		buffer.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"resources/css/Portal.css\" />								").append("\r\n");
	    
		buffer.append("<script type=\"text/javascript\" src=\"resources/javaScript/ux/Portal.js\"></script>							").append("\r\n");
		buffer.append("<script type=\"text/javascript\" src=\"resources/javaScript/ux/PortalColumn.js\"></script>					").append("\r\n");
		buffer.append("<script type=\"text/javascript\" src=\"resources/javaScript/ux/Portlet.js\"></script>						").append("\r\n");
		buffer.append("<script type=\"text/javascript\" src=\"resources/javaScript/ux/TabCloseMenu.js\"></script>					").append("\r\n");
	}
	
	private void initPortalTools(StringBuffer buffer){
		buffer.append("	var tools = [{																								").append("\r\n");
		buffer.append("		id:'gear',																								").append("\r\n");
		buffer.append("		handler: function(){																					").append("\r\n");
		buffer.append("			Ext.Msg.alert('Message', 'The Settings tool was clicked.');											").append("\r\n");
		buffer.append("		}																										").append("\r\n");
		buffer.append("	},{																											").append("\r\n");
		buffer.append("		id:'close',																								").append("\r\n");
		buffer.append(" 	handler: function(e, target, panel){																	").append("\r\n");
		buffer.append("			panel.ownerCt.remove(panel, true);																	").append("\r\n");
		buffer.append("		}																										").append("\r\n");
		buffer.append(" }];																											").append("\r\n");
	}
	
	private void initNorth(StringBuffer buffer){
		buffer.append("	new Ext.BoxComponent({																						").append("\r\n");
		buffer.append("		region:'north',																							").append("\r\n");
		buffer.append("		height: 50,																								").append("\r\n");
		buffer.append("		autoEl: {																								").append("\r\n");
		buffer.append("		tag: 'div',																								").append("\r\n");
		buffer.append("			html:'<p>上方</p>'																					").append("\r\n");
		buffer.append("		}																										").append("\r\n");
		buffer.append("})																											").append("\r\n");
	}
	private void initSouth(StringBuffer buffer){
		buffer.append("	new Ext.BoxComponent({																						").append("\r\n");
		buffer.append("		region:'south',																							").append("\r\n");
		buffer.append("		height: 15,																								").append("\r\n");
		buffer.append("		autoEl: {																								").append("\r\n");
		buffer.append("		tag: 'div',																								").append("\r\n");
		buffer.append("			html:'<p><font size=2><center>版权<center></font></p>'												").append("\r\n");
		buffer.append("		}																										").append("\r\n");
		buffer.append("})																											").append("\r\n");
	}
	
	private void initPortalColunm(StringBuffer buffer){
		buffer.append("	columnWidth:.33,																							").append("\r\n");
		buffer.append("	style:'padding:10px 0 10px 10px',																			").append("\r\n");
		buffer.append("	items:[{																									").append("\r\n");
		buffer.append("		title: 'Grid in a Portlet',																				").append("\r\n");
		buffer.append("		tools: tools,																							").append("\r\n");
		buffer.append("		html: '<p>d</p>'																						").append("\r\n");
		buffer.append("	},{																											").append("\r\n");
		buffer.append("		title: 'Another Panel 1',																				").append("\r\n");
		buffer.append("		tools: tools,																							").append("\r\n");
		buffer.append("		html: '<p>d</p>'																						").append("\r\n");
		buffer.append("	}]																											").append("\r\n");
	}
	
	private void initModuleTree(StringBuffer buffer,ExtTree tree){
		buffer.append("	new Ext.tree.TreePanel({																					").append("\r\n");
		buffer.append("		rootVisible:false,																						").append("\r\n");
		buffer.append("		layout:'fit',																							").append("\r\n");
		buffer.append(" 	autoHeight:false,																						").append("\r\n");
		buffer.append("		autowidth: false,																						").append("\r\n");
		buffer.append("		title: '"+tree.getText()+"',																			").append("\r\n");
		buffer.append("		border: false,																							").append("\r\n");
		buffer.append("		autoScroll: false,																						").append("\r\n");
		buffer.append("		animate: true,																							").append("\r\n");
		buffer.append("		containerScroll:true,																					").append("\r\n");
		buffer.append("		enableDD: false,																						").append("\r\n");
		if(tree.getIconCls()!=null && !"".equals(tree.getIconCls())){
			buffer.append("		iconCls: '"+tree.getIconCls()+"',																		").append("\r\n");
		}
		buffer.append("		root: new Ext.tree.AsyncTreeNode({																		").append("\r\n");
		buffer.append("			draggable : false,																					").append("\r\n");
		buffer.append("			id : '"+tree.getId()+"',																			").append("\r\n");
		if(tree.getChildren()!=null&& tree.getChildren().size()>0){
			buffer.append("children: [{																								").append("\r\n");
            List<ExtTree> tem = tree.getChildren();
            int size = tem.size();
            for(int i=0;i<size;i++){
            	ExtTree t = tem.get(i);
            	buffer.append("text: '"+t.getText()+"',																				").append("\r\n");
            	buffer.append("iconCls: '"+t.getIconCls()+"',																		").append("\r\n");
            	buffer.append("id: '"+t.getId()+"',																					").append("\r\n");
            	buffer.append("url: '"+t.getUrl()+"',																				").append("\r\n");
            	if(t.getChildren()!=null&& t.getChildren().size()>0){
            		buffer.append("leaf: false,																						").append("\r\n");
            		genernateChirldMenu(buffer,t);
            	}else{
            		buffer.append("leaf: true																						").append("\r\n");
            	}
				if(i != size-1){
					buffer.append("},{																								").append("\r\n");
				}
            }
            buffer.append("}]																										").append("\r\n");

		}
		buffer.append("		}),  																									").append("\r\n");  
		buffer.append("		loader: new Ext.tree.TreeLoader({																		").append("\r\n");
		buffer.append("		})																										").append("\r\n");
		buffer.append("		,listeners:{																							").append("\r\n");
		buffer.append("			'click':function(node){addTab(node);}																").append("\r\n");
		buffer.append("		}																										").append("\r\n");
		buffer.append("})																											").append("\r\n");
	}
	
	private void genernateChirldMenu(StringBuffer buffer,ExtTree tree){
		buffer.append("children: [{																									").append("\r\n");
        List<ExtTree> tem = tree.getChildren();
        int size = tem.size();
        for(int i=0;i<size;i++){
        	ExtTree t = tem.get(i);
        	buffer.append("text: '"+t.getText()+"',																					").append("\r\n");
        	buffer.append("iconCls: '"+t.getIconCls()+"',																			").append("\r\n");
        	buffer.append("id: '"+t.getId()+"',																						").append("\r\n");
        	buffer.append("url: '"+t.getUrl()+"',																					").append("\r\n");
        	if(t.getChildren()!=null&& t.getChildren().size()>0){
        		buffer.append("leaf: false,																							").append("\r\n");
        		genernateChirldMenu(buffer,t);
        	}else{
        		buffer.append("leaf: true																							").append("\r\n");
        	}
			if(i != size-1){
				buffer.append("},																									").append("\r\n");
			}
        }
        buffer.append("}]																											").append("\r\n");
	}
	
	private void initClick(StringBuffer buffer){
		buffer.append("	function addTab(node){																						").append("\r\n");
		buffer.append("		var id = node.text+'_'+node.id;																			").append("\r\n");
		buffer.append("		var url = node.attributes.url;																			").append("\r\n");
		buffer.append("		var mainTabs = Ext.getCmp('"+this.getId("tabs_main")+"');												").append("\r\n");
		buffer.append("		if(node.isLeaf()){																			").append("\r\n");
		buffer.append("			var n = mainTabs.getComponent(id);																	").append("\r\n");
		buffer.append("			if (!n) {																							").append("\r\n");
		buffer.append("				n = mainTabs.add({																				").append("\r\n");
		buffer.append("					id:id,																						").append("\r\n");
		buffer.append("					closable:true,																				").append("\r\n");
		buffer.append("					title:node.text,																			").append("\r\n");
		buffer.append("					autoScroll:true,																			").append("\r\n");
		buffer.append("					iconCls:node.attributes.iconCls,															").append("\r\n");
		buffer.append("					layout:'fit',																				").append("\r\n");
		buffer.append("					autoLoad:{url:url+'?parentWinId="+this.getId("tabs_main")+"&winId='+id,scripts:true,nocache:true}").append("\r\n");
		buffer.append("				});																								").append("\r\n");
		buffer.append("			}																									").append("\r\n");
		buffer.append("			mainTabs.setActiveTab(n);																			").append("\r\n");
		buffer.append("		}																										").append("\r\n");
		buffer.append("	}																											").append("\r\n");
	}
	public List<ExtTree> getList() {
		return list;
	}

	public void setList(List<ExtTree> list) {
		this.list = list;
	}
}
